﻿using CommunityToolkit.Mvvm.ComponentModel;
using Company.Core.Interfaces;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using System.Collections.ObjectModel;

namespace Company.Shell.Services
{
    class ConfigProvider : ObservableObject, IConfigProvider
    {
        private Config config = new();
        /// <summary>
        /// 系统参数
        /// </summary>
        public Config Config
        {
            get { return config; }
            set { SetProperty(ref config, value); }
        }

        private ObservableCollection<AlarmItem> alarmItems = new();
        /// <summary>
        /// 报警内容描述
        /// </summary>
        public ObservableCollection<AlarmItem> AlarmItems
        {
            get { return alarmItems; }
            set { SetProperty(ref  alarmItems, value); }
        }

        public ConfigProvider(IConfigManager configManager)
        {
            var temp = configManager.Read<Config>(Core.Enums.ConfigType.System);//读本地系统设置
            if (temp == null)
            {
                configManager.Write(Core.Enums.ConfigType.System, Config);//首次保存
            }
            else
            {
                Config = temp;
            }

            var temp2 = configManager.Read<ObservableCollection<AlarmItem>>(Core.Enums.ConfigType.Alarm);//读报警描述
            if (temp2 == null)
            {
                configManager.Write(Core.Enums.ConfigType.Alarm, AlarmItems);//首次保存
            }
            else
            {
                AlarmItems = temp2;
            }
        }
    }
}
